//
//  HomeIndustryDetailsController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/18.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "HomeIndustryDetailsController.h"
#import "HomeRecomedController.h"
#import "HomeQuestionAndAnswerController.h"
#import "HomeFocusCell.h"
#import "HomeIndustryDetailsModel.h"

@interface HomeIndustryDetailsController ()<UITableViewDelegate,UITableViewDataSource>
/**listView*/
@property (nonatomic,strong)UITableView*listView;
/**<#属性#>*/
@property (nonatomic,strong)NSMutableArray*dataArray;

@end

@implementation HomeIndustryDetailsController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.listView];
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}
#pragma mark --UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 3;
    }
    return self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return UIView.new;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return UIView.new;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RecomedLeftTextRightImageCell * imageCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([RecomedLeftTextRightImageCell class])];
    MoreImageAndTextCell * moreCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MoreImageAndTextCell class])];
    HomeFocusMorenImageCell * userMoreImageCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeFocusMorenImageCell class])];
    
    HomeIndustryDetailsModel * model = self.dataArray[indexPath.row];
    
    if (model.imageNumber.integerValue == 1) {
        imageCell.indusModel = model;
        return imageCell;
    }
    if (model.imageNumber.integerValue == 3) {
        moreCell.indusModel = model;
        return moreCell;
    }
    userMoreImageCell.indusModel = model;
    return userMoreImageCell;
    
    return moreCell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.navigationController pushViewController:[ImageAndTextDetailController new] animated:YES];

}
-(UITableView*)listView{
    if(!_listView){
        _listView=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.tableFooterView = UIView.new;
        _listView.tableHeaderView = UIView.new;
        _listView.rowHeight = UITableViewAutomaticDimension;
        _listView.estimatedRowHeight = 44.0f;

        [_listView registerClass:[RecomedLeftTextRightImageCell class] forCellReuseIdentifier:NSStringFromClass([RecomedLeftTextRightImageCell class])];
        
        [_listView registerClass:[MoreImageAndTextCell class] forCellReuseIdentifier:NSStringFromClass([MoreImageAndTextCell class])];
        [_listView registerClass:[HomeFocusMorenImageCell class] forCellReuseIdentifier:NSStringFromClass([HomeFocusMorenImageCell class])];
        _listView.separatorColor = SeparatorColor;

        
    }
    return _listView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"HomeIndustryDetails" ofType:@"plist"];
           //当数据结构为数组时
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
        
    [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        HomeIndustryDetailsModel *model = [HomeIndustryDetailsModel new];
        [model setValuesForKeysWithDictionary:obj];

        if (model.imageNumber.integerValue > 0) {
            NSMutableArray * imageArray = [NSMutableArray array];
            for (NSInteger i = 0 ; i < model.imageNumber.integerValue; i++) {
                [imageArray addObject:IMAGE_NAMED([Utilis getRandomDynamicImage])];
            }

            model.imageArray = imageArray;
            model.userImage = [Utilis getRandomUserImage];
        }

        [_dataArray addObject:model];
    }];

    }
    return _dataArray;
}
@end
